package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.configs.Constants;
import com.kpx.app.utils.MyCaptureManager;
import com.kpx.app.utils.uiutils.DisplayUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.MyCompoundBarcodeView;
import com.google.zxing.ResultPoint;
import com.journeyapps.barcodescanner.BarcodeCallback;
import com.journeyapps.barcodescanner.BarcodeResult;

import java.util.List;

public class ActivityQRScanner extends ActivityBase implements BarcodeCallback {

    private MyCaptureManager myCaptureManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_qrscanner);
        MyCompoundBarcodeView barcodeView = (MyCompoundBarcodeView) findViewById(R.id.barcodeView);
        int width = DisplayUtil.getScreenWidth(this);
        barcodeView.setLayoutParams(new RelativeLayout.LayoutParams(width, width));
        barcodeView.getStatusView().setVisibility(View.GONE);
        myCaptureManager = new MyCaptureManager(this, barcodeView);
        myCaptureManager.initializeFromIntent(getIntent(), savedInstanceState);
        barcodeView.decodeContinuous(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (myCaptureManager != null) {
            myCaptureManager.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (myCaptureManager != null) {
            myCaptureManager.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myCaptureManager != null) {
            myCaptureManager.onDestroy();
        }
    }

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initData() {

    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        if (myCaptureManager != null) {
            myCaptureManager.onSaveInstanceState(outState);
        }
    }

    @Override
    public void barcodeResult(BarcodeResult barcodeResult) {
        if (Constants.DEBUG) {
            ToastUtil.getInstance().showToast(this, barcodeResult.getText(), Toast.LENGTH_SHORT);
        }
    }

    @Override
    public void possibleResultPoints(List<ResultPoint> list) {

    }
}
